﻿using System;
using System.IO;
using System.Linq;

namespace TestUp.Models.Helpers
{
    public class CourseHelper
    {
        public static string CoursesDirectory
        {
            get
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                path = Path.Combine(path, "App_Data", "TestsData", "Courses");
                return path;
            }
        }

        public Course GetCourse(int courseId)
        {
            return DataWorkItem.Entity.Courses.FirstOrDefault(x => x.CourseId == courseId);
        }

        public string GetCourseDirectoryPath(int courseId)
        {
            var course = GetCourse(courseId);
            if (course != null)
                return GetCourseDirectoryPath(course.CourseName);

            return string.Empty;
        }

        public string GetCourseDirectoryPath(string courseName)
        {
            return Path.Combine(CoursesDirectory, courseName);
        }

        public void DeleteCourse(int courseId)
        {
            var course = GetCourse(courseId);
            if (course != null)
            {
                string dir = GetCourseDirectoryPath(course.CourseName);
                DataWorkItem.Entity.Courses.Remove(course);
                DataWorkItem.Entity.SaveChanges();
                DeleteCourseDirectory(dir);
            }
        }

        public void DeleteCourseDirectory(string path)
        {
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
        }

        public void CreateCourseDirectory(string courseName)
        {
            string path = Path.Combine(CoursesDirectory, courseName);
            if (Directory.Exists(path))
                Directory.Delete(path, true);
            Directory.CreateDirectory(path);
        }

        public void CreateCourse(Course sourceCourse, int ownerUserId)
        {
            var owner = DataHelpers.UserInfoHelper.GetUserInfo(ownerUserId);
            var course = DataWorkItem.Entity.Courses.Add(new Course
                {
                    CourseName = sourceCourse.CourseName
                });
            course.UserInfoes.Add(owner);
            DataWorkItem.Entity.SaveChanges();
            CreateCourseDirectory(course.CourseName);
        }

        public void UpdateCourse(Course newCourse)
        {
            var originalCourse = GetCourse(newCourse.CourseId);
            if (originalCourse != null)
            {
                if (originalCourse.CourseName != newCourse.CourseName)
                {
                    UpdateDirectory(originalCourse.CourseName, newCourse.CourseName);
                }

                //Update fields
                originalCourse.CourseName = newCourse.CourseName;

                DataWorkItem.Entity.SaveChanges();
            }
        }

        private void UpdateDirectory(string sourceName, string newName)
        {
            string oldPath = GetCourseDirectoryPath(sourceName);
            string newPath = GetCourseDirectoryPath(newName);
            if (Directory.Exists(oldPath))
                Directory.Move(oldPath, newPath);
        }
    }
}